import website.settings
import httplib, urllib
import json


def get_data(data):

    ret = '{}'
    try:
        conn = httplib.HTTPConnection(host=website.settings.BUSINESS['host'], port=website.settings.BUSINESS['port'], timeout=1)
        conn.request('POST', website.settings.BUSINESS['url'], json.dumps(data))
        response = conn.getresponse()
        if response.status == 200:
            ret = response.read()
    except:
        pass
    finally:
        if not conn is None:
            conn.close()

    return json.loads(ret)


def get_report(org_costs = None, res_imps = None, res_clicks = None, strategy_costs = None, strategy_imps = None, strategy_clicks = None):
    data = {}
    if not org_costs is None:
        data['organizationCosts'] = org_costs
    if not res_imps is None:
        data['resourceImpressions'] = res_imps
    if not res_clicks is None:
        data['resourceClicks'] = res_clicks
    if not strategy_costs is None:
        data['strategyCosts'] = strategy_costs
    if not strategy_imps is None:
        data['strategyImpressions'] = strategy_imps
    if not strategy_clicks is None:
        data['strategyClicks'] = strategy_clicks

    return get_data(data)

def get_organization_report(ids = []):
    data = {}
    data['organizationCosts'] = ids
    return get_data(data)

def get_resource_report(ids = []):
    data = {}
    data['resourceImpressions'] = ids
    data['resourceClicks'] = ids
    return get_data(data)

def get_strategy_report(ids = []):
    data = {}
    data['strategyCosts'] = ids
    data['strategyImpressions'] = ids
    data['strategyClicks'] = ids
    return get_data(data)





